﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using TeamCommunitySolution.Repositories.Providers;

namespace TeamCommunitySolution.Repositories.Tests.Helpers
{
    public class RepoCleanups
    {
        /// <summary>
        /// Clears the team.
        /// </summary>
        /// <param name="id">The id of the team.</param>
        public static void ClearTeam(string id)
        {
            using (ISession session = Nhibernate.Factory.OpenSession())
            {
                var scope = session.BeginTransaction();
                session.Delete(string.Format("from Team t where t.Id = '{0}'", id));
                scope.Commit();
            }
        }

        /// <summary>
        /// Clears the user.
        /// </summary>
        /// <param name="username">The username.</param>
        public static void ClearUser(string username)
        {
            using (ISession session = Nhibernate.Factory.OpenSession())
            {
                var scope = session.BeginTransaction();
                session.Delete(string.Format("from SystemUser su where su.Email = '{0}'", username));
                scope.Commit();
            }
        }

        /// <summary>
        /// Clears the inbox.
        /// </summary>
        /// <param name="id">The id of the message.</param>
        public static void ClearInbox(string id)
        {
            using (ISession session = Nhibernate.Factory.OpenSession())
            {
                var scope = session.BeginTransaction();
                session.Delete(string.Format("from MessageInbox mi where mi.Id = '{0}'", id));
                scope.Commit();
            }
        }

        /// <summary>
        /// Clears the outbox.
        /// </summary>
        /// <param name="id">The id of the message.</param>
        public static void ClearOutbox(string id)
        {
            using (ISession session = Nhibernate.Factory.OpenSession())
            {
                var scope = session.BeginTransaction();
                session.Delete(string.Format("from MessageOutbox mo where mo.Id = '{0}'", id));
                scope.Commit();
            }
        }

        /// <summary>
        /// Clears the team invite.
        /// </summary>
        /// <param name="id">The id of the invite.</param>
        public static void ClearTeamInvite(string id)
        {
            using (ISession session = Nhibernate.Factory.OpenSession())
            {
                var scope = session.BeginTransaction();
                session.Delete(string.Format("from TeamInvite ti where ti.Id = '{0}'", id));
                scope.Commit();
            }
        }

        /// <summary>
        /// Clears the venue.
        /// </summary>
        /// <param name="id">The id of the venue.</param>
        public static void ClearVenues(string id)
        {
            using (ISession session = Nhibernate.Factory.OpenSession())
            {
                var scope = session.BeginTransaction();
                session.Delete(string.Format("from Venue v where v.Id = '{0}'", id));
                scope.Commit();
            }
        }

        /// <summary>
        /// Clears the metric groups.
        /// </summary>
        /// <param name="id">The id of the group.</param>
        public static void ClearMetricGroup(string id)
        {
            using (ISession session = Nhibernate.Factory.OpenSession())
            {
                var scope = session.BeginTransaction();
                //session.Delete(string.Format("from MetricDefinition md where md.Group.Id = '{0}'", id));
                session.Delete(string.Format("from MetricGroup mg where mg.Id = '{0}'", id));
                scope.Commit();
            }
        }
    }
}
